from django import template
from pagemagic.models import Page

register = template.Library()

from django.contrib.admindocs.views import get_root_path
model_admin_url = '%s%s/%s/' % (get_root_path(), 'pagemagic', Page.__name__.lower()) #@UndefinedVariable

@register.inclusion_tag('pagemagic/admin/menu_branch.html')
def show_admin_menu_branch(parent=None, forloop=None):
    items, level = Page.items_and_level(parent)
    return {
        'parent': parent,
        'parent_forloop': forloop,
        'items': items,
        'level': level,
        'padding_level': level * 2,
        'level_range': range(level),
        'model_admin_url': model_admin_url,
    }

@register.simple_tag
def global_cycle(a=None, b=None):
    if a is None:
        global_cycle.counter = 0
        return ''

    global_cycle.counter += 1
    if global_cycle.counter % 2:
        return a
    else:
        return b

global_cycle.counter = 0
